package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn); //注入连接
        boolean isRight = false;
        try {
            isRight = guestbookDao.add(guestbook); //调用dao的方法
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> queryAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn); //注入连接
        return guestbookDao.getAll();
    }

    @Override
    public boolean deleteBean(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn); //注入连接
        boolean isRight = false;
        try {
            isRight = guestbookDao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public Guestbook getQueryById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn); //注入连接
        return guestbookDao.getQueryById(id);
    }

    @Override
    public boolean update(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn); //注入连接
        boolean isRight = false;
        try {
            isRight = guestbookDao.update(gb);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }


    public static void main(String[] args) {
            GuestbookService service = new GuestbookServiceImpl();
            List<Object> list  =service.queryAll();
            for(int i = 0;i<list.size();i++) {
                Guestbook gb = (Guestbook) list.get(i);
                System.out.println(gb.getName());
            }
//            Guestbook gb = new Guestbook();
//            gb.setName("xyz");
//            gb.setPhone("12121212");
//            gb.setEmail("xyz@163.com");
//            gb.setTitle("test......");
//            gb.setContent("content");
//            Date date = new Date();
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            gb.setCreatedtime(simpleDateFormat.format(date));
//            boolean isRight = service.save(gb);
//            System.out.println(isRight);
    }
}
